package com.botao.tip.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.botao.tip.entity.Purchase;
import com.botao.tip.entity.User;
import com.botao.tip.service.PurchaseService;
import com.botao.tip.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : vip865047755@126.com
 * @version :JDK1.8
 * @date : 2020-12-03 12:55
 * @File : PurchaseController.java
 * @software: IntelliJ IDEA
 */
@RestController
@RequestMapping("/api/purchase/")
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;

    @GetMapping("queryAll")
    public AjaxResult queryAll(@RequestParam int currentPage, @RequestParam int pageSize) {
        Page<Purchase> page = purchaseService.page(new Page<>(currentPage, pageSize));

        List<Purchase> records = page.getRecords();
        List<Purchase> list = new ArrayList<>(records);

        long total = page.getTotal();
        long current = page.getCurrent();
        long size = page.getSize();
        boolean hasNext = page.hasNext();
        boolean hasPrevious = page.hasPrevious();

        HashMap<Object, Object> map = new HashMap<>();
        map.put("purchase", list);
        map.put("total", total);
        map.put("current", current);
        map.put("size", size);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return AjaxResult.success(map);
    }

    @DeleteMapping("delete/{id}")
    public AjaxResult delete(@PathVariable String id) {
        return purchaseService.removeById(id) ? AjaxResult.success() : AjaxResult.error();
    }


    @RequestMapping("update")
    public AjaxResult update(@RequestParam("id") String id,
                             @RequestParam("title") String title,
                             @RequestParam("product") String product,
                             @RequestParam("supplier") String supplier,
                             @RequestParam("tel") String tel,
                             @RequestParam("signingTime") String signingTime,
                             @RequestParam("state") String state,
                             @RequestParam("manager") String manager) {
        Boolean aBoolean = purchaseService.updatePurchase(id, title, product, supplier, tel, signingTime, state, manager);
        return aBoolean ? AjaxResult.success() : AjaxResult.error(500, "更新失败");
    }

    @RequestMapping("insert")
    public AjaxResult insert(
            @RequestParam("title") String title,
            @RequestParam("product") String product,
            @RequestParam("supplier") String supplier,
            @RequestParam("tel") String tel,
            @RequestParam("signingTime") String signingTime,
            @RequestParam("state") String state,
            @RequestParam("manager") String manager) {
        Boolean aBoolean = purchaseService.insert(title, product, supplier, tel, signingTime, state, manager);
        return aBoolean ? AjaxResult.success("插入成功。") : AjaxResult.error(500, "插入失败");
    }

    @GetMapping("queryOne")
    public AjaxResult queryOne(String id) {
        Purchase purchase = purchaseService.getOne(new QueryWrapper<Purchase>().eq("id", Integer.parseInt(id)));
        if (purchase == null) {
            return AjaxResult.error("id为空。");
        }
        return AjaxResult.success(purchase);
    }

}
